#!/usr/bin/env bash


if ! hash pm2 2>/dev/null ; then
yarn global add pm2
fi

cd $_DIR
pm2 stop ecosystem.config.js -s
pm2 logs --lines 0 `basename $_DIR` --raw &
pm2 start

PM2_PID=$!

cleanup () {
if [ -e /proc/$PM2_PID ]; then
  kill -9 $PM2_PID || echo "pid $PM2_PID is killed"
  pm2 stop ecosystem.config.js -s &&\
  pm2 delete ecosystem.config.js -s
  exit 0
fi
}
trap cleanup EXIT INT TERM

wait

